from PySide2.QtCore import Qt
from PySide2.QtWidgets import QTableWidgetItem, QWidget, QAbstractItemView, QHeaderView

# QtableWidget工具类
class TableWidgetUtil:

    # 渲染列表 (待处理tableWidget，渲染数组,任务头对应列表)
    def render(self,tableWidget,data_list,head):
        # 禁用边框
        tableWidget.setShowGrid(False)

        #         检查列表是否正确(是否登等长)
        for index,item in enumerate(data_list):
            if not (len(item) == len(data_list[0])):
                raise Exception(print("第" + str(index) + "行数据长度与其他不同"))

        # 清空列表
        tableWidget.clearContents()
        # 获取表头列表
        headList = [item["key"] for item in head]
        # 获取表体键
        head = [item["value"] for item in head]
        #         设置表格行列数
        tableWidget.setRowCount(len(data_list))
        tableWidget.setColumnCount(len(headList))
        # 设置表头
        tableWidget.setHorizontalHeaderLabels(headList)
        #         开始渲染
        #         遍历行
        for index,item in enumerate(data_list):
            #             遍历列
            for headIndex,headItem in enumerate(head):
                content = item[str(headItem)]
                if content == None:
                    content = " "

                # 如元素是字符或数字
                if isinstance(content,str):
                    tableWidget.setItem(index, headIndex, QTableWidgetItem(str(content)))
                    # 内容居中
                    tableWidget.item(index, headIndex).setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter)
                elif isinstance(content,int) or isinstance(content,float):
                    tableWidget.setItem(index, headIndex, QTableWidgetItem(str(content)))
                    # 内容居中
                    tableWidget.item(index, headIndex).setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter)
                elif isinstance(content,QWidget):
                    tableWidget.setCellWidget(index, headIndex, content)
                    tableWidget.setItem(index, headIndex,QTableWidgetItem(""))
                else:
                    raise Exception(print("请检查" + str(index) + "行" + str(headIndex) + "列数据"))

    # 单元格平铺，可选，可修改设定 (是否可以平铺，是否可选中，是否可编辑)
    def item_pitchOn(self,qtableWidget,pp=True,xz=True,bj=True):
        if pp:
            # 单元格平铺
            qtableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        if xz:
            # 禁止选中
            qtableWidget.setSelectionMode(QAbstractItemView.NoSelection)
        if bj:
            # 禁止编辑
            qtableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)

    # 设置表头颜色及显示 h(横标头是否设置) v(纵表头是否设置)
    def head_color_show(self,qtableWidget,h=True,v=True,q=True,r=True):
        if h:
            qtableWidget.horizontalHeader().setStyleSheet("QHeaderView::section {background-color:#FFFFFF;color: #33384B;padding: 0 10px;border: 0px;border-bottom:1px solid #A0A0A0;}")
            qtableWidget.horizontalHeader().setVisible(True);
        if v:
            qtableWidget.verticalHeader().setStyleSheet("QHeaderView::section {background-color:#FFFFFF;color: #33384B;border: 0px;}")
            qtableWidget.verticalHeader().setDefaultAlignment(Qt.AlignCenter);
            qtableWidget.verticalHeader().setVisible(True);
        if q:
            # 设置列宽自适应
            qtableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
        if r:
            qtableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)

    # 根据第列查询行
    def fineById(self,qtableWidget,id):
        rowCount = qtableWidget.rowCount()
        for index in range(rowCount):
            text = qtableWidget.item(index, 0).text()
            if text == id:
                return index

    # 获取整行数据
    def get_row_by_rowIndex(self,qtableView,rowIndex):
        # 获取表格的列数
        columnCount = qtableView.model().columnCount()
        # 获取该行数据
        row_data = [qtableView.model().item(rowIndex, index).text() for index in range(columnCount)]
        return row_data